package com.tongji.housediy.view.titlewindows
{
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.model.GoodsModel;
	import com.tongji.housediy.model.SelectedObjectModel;
	import com.tongji.housediy.utils.DragDropManager;
	import com.tongji.housediy.utils.PopUpAdmin;
	import com.tongji.housediy.view.components.AbstractBorderChild;
	import com.tongji.housediy.view.components.Furniture;
	import com.tongji.housediy.view.components.NormalDoor;
	import com.tongji.housediy.view.components.NormalWindow;
	import com.tongji.housediy.view.components.RoomShape;
	import com.tongji.housediy.vo.Goods;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.TitleWindowBoundsEvent;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class GoodsWindowMediator extends Mediator
	{
		[Inject]
		public var view:GoodsTitleWindow;
		
		[Inject]
		public var selectedModel:SelectedObjectModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		[Embed(source="assets/images/warning.png")]
		private var BulletCritical:Class;
		
		override public function onRegister():void
		{
			view.addEventListener(TitleWindowBoundsEvent.WINDOW_MOVING, windowMovingHandler);
			
			view.infoBtn.addEventListener(MouseEvent.CLICK, infoBtnClickHandler);
			view.deleteBtn.addEventListener(MouseEvent.CLICK, deleteBtnClickHandler);
			view.rotationBtn.addEventListener(MouseEvent.CLICK, rotationBtnClickHandler);
			view.copyBtn.addEventListener(MouseEvent.CLICK, copyBtnClickHandler);
		}
		
		private function windowMovingHandler(event:TitleWindowBoundsEvent):void
		{
			dispatch(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.UPDATE_LINE_POSITION, 0, 0, view.x + view.width / 2, view.y + view.height / 2));
		}
		
		private function infoBtnClickHandler(event:MouseEvent):void
		{
			PopUpAdmin.popupCenterWindow("furnitureInfo", view, mediatorMap);
			var selectedObj:RoomShape = selectedModel.selectedObj as RoomShape;
			goodsModel.arrangeSizes(selectedObj.hostGoods);
		}
		
		private function deleteBtnClickHandler(event:MouseEvent):void
		{
			var alert:Alert;
			if (selectedModel.selectedObj is Furniture)
			{
				alert = Alert.show("确定要删除吗？", "删除家具?", Alert.OK | Alert.CANCEL, view.stage as Sprite, alertClickHandler);
			}
			else if (selectedModel.selectedObj is NormalDoor)
			{
				alert = Alert.show("确定要删除吗？", "删除门?", Alert.OK | Alert.CANCEL, view.stage as Sprite, alertClickHandler);
			}
			else if (selectedModel.selectedObj is NormalWindow)
			{
				alert = Alert.show("确定要删除吗？", "删除窗?", Alert.OK | Alert.CANCEL, view.stage as Sprite, alertClickHandler);
			}		
			alert.titleIcon = BulletCritical;
		}
		
		private function alertClickHandler(event:CloseEvent):void
		{
			if (event.detail == Alert.OK)
			{
				selectedModel.deleteSelectedObj();
			}
		}
		
		private function rotationBtnClickHandler(event:MouseEvent):void
		{
			if (selectedModel.selectedObj is Furniture)
			{
				view.moveArea.visible = true;
				view.moveArea.addEventListener(KeyboardEvent.KEY_DOWN, changeVisible);				
			}
			else
				PopUpAdmin.popupCenterWindow("doorRotation", view, mediatorMap, false);

		}
		
		private function changeVisible(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.ENTER)
			{
				view.moveArea.visible =false;
				(selectedModel.selectedObj as Furniture).rotationFurniture(Number(view.textInput.text));
				dispatch(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW));
			}
		}
		
		private function copyBtnClickHandler(event:MouseEvent):void
		{
			if (selectedModel.selectedObj is Furniture)
			{
				var selectedFurniture:Furniture = selectedModel.selectedObj as Furniture;
				DragDropManager.startDragFurniture(view, event, selectedFurniture.clone());
			}
			else
				DragDropManager.startDragBorderChild(view, event,
					(selectedModel.selectedObj as AbstractBorderChild).dragType,
					(selectedModel.selectedObj as AbstractBorderChild).clone());
			
			dispatch(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW));
		}
	}
}